import 'package:favoritecate/core/model/meal_model.dart';
import 'package:favoritecate/core/viewmodel/favor_meal_viewmodel.dart';
import 'package:favoritecate/ui/pages/detail/meal_detail.dart';
import 'package:favoritecate/ui/widgets/operation_item.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';


class FYMealItem  extends StatelessWidget {
  FYMealModel _model;

  FYMealItem(this._model);

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsets.all(10),
      elevation: 4, //阴影
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      child: Column(
        children: <Widget>[
          buildImageWidget(context),
          buildOperationWidget()
        ],
      ),
    );
  }

  Widget buildImageWidget(BuildContext context) {
    return GestureDetector(
      child: Stack(
        children: <Widget>[
          ClipRRect(
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(12),
                topRight: Radius.circular(12)
            ),
            child: Image.network(_model.imageUrl, width: double.infinity,
              height: 250,
              fit: BoxFit.cover,),
          ),
          Positioned(
            right: 10,
            bottom: 10,
            child: Container(
              width: 300,
              padding: EdgeInsets.fromLTRB(20, 5, 0, 5),
              decoration: BoxDecoration(
                  color: Colors.black54,
                  borderRadius: BorderRadius.circular(6)
              ),
              child: Text(_model.title, style: Theme
                  .of(context)
                  .textTheme
                  .display3
                  .copyWith(color: Colors.white),),
            ),
          )
        ],
      ),
      onTap: () {
        Navigator.of(context).pushNamed(
            FYMealDetailPage.routeName, arguments: _model);
      },
    );
  }

  Widget buildOperationWidget() {
    return Padding(
      padding: EdgeInsets.all(16),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          OperationItem(Icon(Icons.schedule), '${_model.duration}分钟'),
          OperationItem(Icon(Icons.restaurant), '${_model.complexStr}'),
          buildFavorOperationItem()
        ],
      ),
    );
  }

  Widget buildFavorOperationItem() {
    return Consumer<FYFavorMealViewModel>(
      builder: (ctx,favor,child){
        IconData iconData = favor.isFavor(_model) ? Icons.favorite : Icons.favorite_border;
        String str = favor.isFavor(_model) ? '收藏' : '未收藏';
        Color color = favor.isFavor(_model) ? Colors.red : Colors.black;
        return GestureDetector(
          child: OperationItem(Icon(iconData,color: color,),str),
          onTap: (){
            favor.handleMeal(_model);
          },
        );
      },
    );
  }
}

